/*
Disclaimer of Warranty: THE PACKAGE IS PROVIDED
BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS'
AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
THE IMPLIED WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT
ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL LAW.
UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
OR CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF
THE PACKAGE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


(c) Copyright 2013 Edward Principe
All Rights Reserved

*/
#ifndef TOKMOD_H
#define TOKMOD_H

#include <fstream>
#include <list>
#include "tokline.h"
#include "rex.h"

/////////////////////////////////////////////////////////////////////
// TokMod: A module of tokens
/////////////////////////////////////////////////////////////////////
class TokMod
{
public: // Construction
	TokMod() {}

	TokMod(const TokMod& rhs)
	{
		Copy(rhs);
	}

public: // Methods
	void Set(const TokLine& rhs); // Sets tokens
	void Set(const TokNode& rhs);

	void SetLine(unsigned long line)
	{
		_buffer->SetLine(line);
	}

public: // Methods
	void Append(const TokMod& rhs);
	void Append(const TokLine& rhs);
	void Append(const TokNode& rhs);

public: // Methods
	void Commit(); // Adds the line buffer to the module. For adding tokens

public: // Methods
	void Reset()
	{
		_tokens.erase(_tokens.begin(), _tokens.end());
	}

public: // Methods
	list<TokLine>::iterator GetBegin()
	{
		return _tokens.begin();
	}

	list<TokLine>::iterator GetEnd()
	{
		return _tokens.end();
	}

public: // Methods
	void Read(fstream& in); // Reads in a module
	void Write(fstream& out); // Writes a module

#ifdef _DEBUG
public: // Debug methods
	void Dump();
#endif

public: // Operators
	TokMod& operator = (const TokMod& rhs)
	{
		Copy(rhs);
		return *this;
	}

	TokMod& operator = (const TokLine& rhs)
	{
		_tokens.push_back(rhs);
		return *this;
	}

	TokMod& operator = (const TokNode& rhs)
	{
		_buffer->Set(rhs);
		return *this;
	}

	TokMod& operator += (const TokMod& rhs)
	{
		Append(rhs);
		return *this;

	}

	TokMod& operator += (const TokLine& rhs)
	{
		Append(rhs);
		return *this;
	}

	TokMod& operator += (const TokNode& rhs)
	{
		Append(rhs);
		return *this;
	}

	TokMod& operator + (const TokMod& rhs)
	{
		Append(rhs);
		return *this;
	}

	TokMod& operator + (const TokLine& rhs)
	{
		Append(rhs);
		return *this;
	}

	TokMod& operator + (const TokNode& rhs)
	{
		Append(rhs);
		return *this;
	}

private: // Methods
	void Init() {} // Initlizes the class
	void Copy(const TokMod& rhs)
	{
		if (this == &rhs)
			return;

		_tokens = rhs._tokens;
	}

private: // Attributes
	list<TokLine> _tokens; // A module of tokens
	SmtPtr<TokLine> _buffer; // A buffer to set tokends
};
#endif